package top.healthylife.service.impl;/*
 *@Author GaoZeXi
 *@Created time 2020/4/24 11:12
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.healthylife.domain.Permission;
import top.healthylife.entity.PageResult;
import top.healthylife.entity.QueryPageBean;
import top.healthylife.mapper.PermissionMapper;
import top.healthylife.service.PermissionService;

import java.util.List;

@Service(interfaceClass = PermissionService.class )
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        int currentPage = queryPageBean.getCurrentPage();
        int pageSize =  queryPageBean.getPageSize();
        //开启分页
        PageHelper.startPage(currentPage,pageSize);
        String queryString  = queryPageBean.getQueryString();
        Page<Permission> page = permissionMapper.selectByCondition(queryString);
        System.out.println("总记录=============="+page.getTotal());
        System.out.println("结果集=============="+page.getResult());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    @Override
    public void deleteById(Integer id) {
        //检查是否有角色包含该权限
        long count =permissionMapper.selectCountByPermissionId(id);
        if (count>0){
            throw new RuntimeException("当前权限项被引用,不能删除");
        }
        permissionMapper.deleteById(id);

    }

    @Override
    public void updateById(Permission permission) {
        permissionMapper.updateById(permission);
    }

    @Override
    public List<Permission> findAll() {
        return permissionMapper.findAll();
    }

    @Override
    public Permission findById(Integer id) {
        return permissionMapper.findById(id);
    }
}
